#include "list.h"

List* BuyListNode(DataType x)
{
    List* newnode = new List;
    newnode->data = x;
    newnode->next = NULL;
    newnode->prev = NULL;

    return newnode;
}


List* ListInit()
{
    List* phead = BuyListNode(0);

    phead->next = phead;
    phead->prev = phead;

    return phead;
}


void DeleteList(List* phead)
{
    assert(phead);
    List* cur = phead->next;
    while(cur != phead)
    {
        List* next = cur->next;
        delete(cur);
        cur = next;
    }

    delete(cur);
    phead = NULL;
    
}


List* FindNode(List* phead, DataType x)
{
    List* cur = phead->next;
    while(cur != phead)
    {
        if(cur->data == x)
        {
            return cur;
        }
        cur = cur->next;
    }

    return NULL;
}


void ListInsert(List* pos, DataType x)
{
    assert(pos);
    List* newnode = BuyListNode(x);

    newnode->next = pos;
    newnode->prev = pos->prev;
    pos->prev->next = newnode;
    pos->prev = newnode;
}


void ListErase(List* pos)
{
    assert(pos);
    pos->next->prev = pos->prev;
    pos->prev->next = pos->next;

    delete(pos);
}


void ListPushBack(List* phead, DataType x)
{
    ListInsert(phead, x);
}


void ListPopBack(List* phead)
{
    ListErase(phead->prev);
}


void ListPushFront(List* phead, DataType x)
{
    List* first = phead->next;
    ListInsert(first, x);
}


void ListPopFront(List* phead)
{
    ListErase(phead->next);
}



























